﻿using System;

namespace ConsoleDemo.Polymorphism
{
    public class OverrideDemo
    {
        // 子类对父类成员的重写。
        // 因为类成员个数还是那么多，只是更新版本，所以又称为纵向扩展。
        public static void Call()
        {
            var car = new Car();
            car.Run();              // Car is running!

            var v = new Vehicle();
            v.Run();                // I'm running!

        }

        // C# 支持用父类类型的变量引用子类类型的实例。
        // 函数成员的具体行为（版本）由对象决定。
        public static void Call2()
        {
            Vehicle v = new RaceCar();
            v.Run();                    // Race car is running!

            Car c = new RaceCar();
            c.Run();                    // Race car is running!
        }
    }

    class Vehicle
    {
        public virtual void Run()
        {
            Console.WriteLine("I'm running!");
        }
    }

    class Car : Vehicle
    {
        public override void Run()
        {
            Console.WriteLine("Car is running!");
        }
    }

    class RaceCar : Car
    {
        public override void Run()
        {
            Console.WriteLine("Race car is running!");
        }
    }
}
